package me.xiaosiqi.service.system.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.xiaosiqi.dao.system.ModuleDao;
import me.xiaosiqi.dao.system.ModuleDao;
import me.xiaosiqi.domain.system.Module;
import me.xiaosiqi.factory.MapperFactory;
import me.xiaosiqi.service.system.ModuleService;
import me.xiaosiqi.service.system.ModuleService;
import me.xiaosiqi.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author xiaosiqi
 * @date 2021/6/3 14:38
 */
public class ModuleServiceImpl implements ModuleService {

    @Override
    public void save(Module module) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            String s = UUID.randomUUID().toString();
            module.setId(s);
            moduleDao.save(module);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(Module module) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            moduleDao.delete(module);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(Module module) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            moduleDao.update(module);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Module findById(String id) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findById(id);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Module> findAll() {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findAll();
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public PageInfo findPage(int page, int size) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            PageHelper.startPage(page,size);
            List<Module> all = moduleDao.findAll();
            return new PageInfo(all);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Map>  findAuthorDataByRoleId(String id) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findAuthorDataByRoleId(id);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void updateRoleModule(String roleId, String[] moduleIds) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            moduleDao.deleteRoleModule(roleId);

            moduleDao.saveRoleModule(roleId, moduleIds);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
